//
//  SettingController.m
//  MidProject
//
//  Created by itlab on 2020/11/15.
//  Copyright © 2020 itlab. All rights reserved.
//

#import "SettingViewController.h"
#import "NotificationViewController.h"
@interface SettingViewController()
@property(nonatomic, strong) UITableView *tableView;
@property(nonatomic, strong) NSDictionary *jsonDictionaryOrArray;
@property(nonatomic, assign) bool login_state;
@end

@implementation SettingViewController

-(void)viewDidLoad{
    [super viewDidLoad];
    // 跳到当前的页面页面首先需要清楚所有的subview
    [[self.view subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];

    //在登录之后获取用户的信息
    NSString *urlString = @"http://172.18.178.56/api/user/info/self";
    NSURL *url = [NSURL URLWithString:urlString];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"GET";
    request.timeoutInterval = 60;
    NSURLSession *session = [NSURLSession sharedSession];
    dispatch_semaphore_t semaphore= dispatch_semaphore_create(0);
    NSURLSessionDataTask  *dataTask = [session dataTaskWithRequest:request
                                                 completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                     NSError *jsonError;
                                                     self->_jsonDictionaryOrArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&jsonError];
                                                     if(jsonError) {
                                                         // check the error description
                                                         NSLog(@"json error : %@", [jsonError localizedDescription]);
                                                     } else {
                                                         // use the jsonDictionaryOrArray
                                                         NSLog(@"json : %@",self->_jsonDictionaryOrArray[@"Email"]);
                                                         self->_login_state = true;
                                                     }
                                                     dispatch_semaphore_signal(semaphore);
                                                 }];
    [dataTask resume];
    
    NSLog(@"%@",_jsonDictionaryOrArray);
    
    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
    
    self.title = @"设置中心";
    self.view.backgroundColor = [UIColor whiteColor];
    _tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_tableView];
}

//可选实现
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;  // 这里将UITableView分成2个分区
}

//
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    // 设置每个section的row数量(都是从0下标开始)
    // 设置了2个section
    if (section == 0) {
        return 1;
    }else{
        return 3;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    // cell的重用标识(*******************不是很懂,不知道是不是为了标识各种不同的cell)
    //    static NSString * cellIdentifier  = @"cell";
    //    // 从重用队列中取出cell对象
    //    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    //    // 如果没有,则创建(解释:一般刚进入界面的时候,是不需要重用的,当时显示的是能够映入界面的足够的cell,只有拖动的时候,才需要)
    //    if (!cell) {
    //        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:cellIdentifier];
    //    }
    
    static NSString *CellIdentifier = @"Cell";
    // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //改为以下的方法
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; //根据indexPath准确地取出一行，而不是从cell重用队列中取出
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    
    if(_login_state){
        if(indexPath.section == 0 && indexPath.row == 0){
            //        cell.userInteractionEnabled = NO;
            //用户的头像
            cell.imageView.image = [UIImage imageNamed:@"userImg_selected.png"];
            //用户的昵称
            cell.textLabel.text = self->_jsonDictionaryOrArray[@"Name"];
            cell.detailTextLabel.text = self->_jsonDictionaryOrArray[@"Email"];
        }else if(indexPath.section == 1){
            if(indexPath.row == 0){
                cell.userInteractionEnabled = NO;
                cell.imageView.image = [UIImage imageNamed:@"storeImg.png"];
                cell.textLabel.text = @"存储额度";
                float maxSize = [self->_jsonDictionaryOrArray[@"MaxSize"] floatValue] /(1024*1024);
                float usedSize = [self->_jsonDictionaryOrArray[@"UsedSize"] floatValue] /(1024*1024);
                NSString *mString = [NSString stringWithFormat:@"%3f",maxSize];
                NSString *uString = [NSString stringWithFormat:@"%3f",usedSize];
                NSString *res = [NSString stringWithFormat:@"全部空间 %@GB 已用空间 %@GB",mString,uString];
                cell.detailTextLabel.text = res;
            }
            else if(indexPath.row == 1){
                cell.userInteractionEnabled = NO;
                cell.imageView.image = [UIImage imageNamed:@"starImg.png"];
                cell.textLabel.text = @"收藏";
            }
            else if(indexPath.row == 2){
                cell.imageView.image = [UIImage imageNamed:@"squareImg.png"];
                cell.textLabel.text = @"通知";
            }
        }
        return cell;
    }else{
        if(indexPath.section == 0 && indexPath.row == 0){
            //cell.userInteractionEnabled = NO;
            //用户的头像
            cell.imageView.image = [UIImage imageNamed:@"userImg.png"];
            //用户的昵称
            cell.textLabel.text = @"请登录";
        }else if(indexPath.section == 1){
            if(indexPath.row == 0){
                cell.userInteractionEnabled = NO;
                cell.imageView.image = [UIImage imageNamed:@"storeImg.png"];
                cell.textLabel.text = @"存储额度";
                cell.detailTextLabel.text = @"未知";
            }
            else if(indexPath.row == 1){
                cell.userInteractionEnabled = NO;
                cell.imageView.image = [UIImage imageNamed:@"starImg.png"];
                cell.textLabel.text = @"收藏";
            }
            else if(indexPath.row == 2){
                cell.userInteractionEnabled = NO;
                cell.imageView.image = [UIImage imageNamed:@"squareImg.png"];
                cell.textLabel.text = @"通知";
            }
        }
        return cell;
    }
}

// section的title个人信息和功能
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    //    return [NSString stringWithFormat:@"%i",section+1];
    if(section == 0)
    {
        return[NSString stringWithFormat:@"个人信息"];
    }
    else{
        return[NSString stringWithFormat:@"功能"];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 0){
        return 80;
    }else{
        return 110;
    }
}

-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    if(indexPath.section == 0){
        if(indexPath.row == 0){
            self.tabBarController.selectedIndex = 0;
            //需要添加所有的登录注册页面
            
            
        }
    }
    if(indexPath.section == 1){
        //选中收藏cell
        if(indexPath.row == 1){
            //还没有做收藏页面
        }
        //选中通知cell
        if(indexPath.row == 2){
            //跳转到通知页面
            NotificationViewController *controller = [[NotificationViewController alloc]init];
            [[self navigationController] pushViewController:controller animated:YES];
        }
    }
}

// 隐藏多余的分割线
//- (void)_setExtraCellLineHidden:(UITableView *)tableView
//{
//    UIView *view =[ [UIView alloc]init];
//    view.backgroundColor = [UIColor clearColor];
//    [tableView setTableFooterView:view];
//}

- (void) viewWillAppear:(BOOL)animated {
    [self viewDidLoad];
}

@end
